
function dataFrmDB() {
    this.indivs = new Array();
    this.ticks = new Array();
}

function attr(){
    this.name;
    this.id;
}

function indiv(){
    this.name;
    this.attrs = new Array();
}

function createDataFrmTable(tableId){
	var mDataFrmDB = new dataFrmDB();
	
	var trs = $("#" + tableId + " tr");
	$(trs).each(function(index){
		var tds = $(this).find("td");
			var index = 0;
			var mIndiv = new indiv();
			$(tds).each(function(index){
				if(index == 0){
					
					mIndiv.name = $(this).text();
				        mDataFrmDB.ticks.push($(this).text());
				}
				else{
					var x = $(this).text();
					mIndiv.attrs.push($(this).text());
				}
			})
			
			mDataFrmDB.indivs.push(mIndiv);
	})
	
	return mDataFrmDB;
}

function result(){
this.ticks = new Array();
this.resultArray = new Array();
}

function DBFormat(dataFrmDB) {
    var attCount = dataFrmDB.indivs[0].attrs.length;
    var mResult = new result(); 
    mResult.ticks = dataFrmDB.ticks;

    var baseNum = 0;

    var baseNums = new Array();
    for (var n = 0; n < dataFrmDB.indivs.length; ++n) {
        baseNums.push(0);
    }

    for (var j = 0; j < attCount; ++j) {
        var ary = new Array();
        mResult.resultArray.push(ary);
    }
    
    for (var i = 0; i < attCount; ++i) {
        for (var k = 0; k < dataFrmDB.indivs.length; ++k) {
            var atrVal = dataFrmDB.indivs[k].attrs[i];
            var intatrVal = parseFloat(atrVal);
            mResult.resultArray[i].push(intatrVal/* - baseNums[k]*/);
            baseNums[k] = atrVal;
        }
        baseNum = 0;
    }
	
    return mResult;
}


function stackData(){

	this.s1 = [1,2]; 
	this.s2 = [1,1];
	this.s3 = [1,1];

	this.array = new Array();
	this.array.push(this.s1);
	this.array.push(this.s2);
	this.array.push(this.s3);
	this.divName = "chart3";
}



// hanshukaitou
function drawStackChart(data){
	debugger;
	var ticks = data.ticks;

	debugger;
    
	var s = $.jqplot.BarRenderer;
	var st = new stackData();
	var ss = st.array;
	plot3 = $.jqplot("chart3",/*data.resultArray*/data.resultArray, {
            // Tell the plot to stack the bars.
            stackSeries: true,
            captureRightClick: true,
            seriesDefaults: {
                renderer: $.jqplot.BarRenderer,
                rendererOptions: {
                    // Put a 30 pixel margin between bars.
                    barMargin: 5,
		    barWidth:10,
                    // Highlight bars when mouse button pressed.
                    // Disables default highlighting on mouse over.
                    highlightMouseDown: true
                },
                pointLabels: { show: true }
            },
            axes: {
                xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: ticks
                },
                yaxis: {
                    // Don't pad out the bottom of the data range.  By default,
                    // axes scaled as if data extended 10% above and below the
                    // actual range to prevent data points right on grid boundaries.
                    // Don't want to do that here.
                    padMin: 0,
                    pad: 1.2,
                    min:0
                   
                }
            },
            legend: {
                show: true,
                location: 'e',
                placement: 'outside'
            }
            ,
            series: [{ label: 'attr1' }, { label: 'attr2' }, { label: 'attr3' }, { label: 'attr4' }, { label: 'attr5' },
                     { label: 'attr6' }, { label: 'attr7' }, { label: 'attr8' }, { label: 'attr9' }, { label: 'attr10' },
                     { label: 'attr11' }
                     ]
        });
        // Bind a listener to the "jqplotDataClick" event.  Here, simply change
        // the text of the info3 element to show what series and ponit were
        // clicked along with the data for that point.
        $('#chart3').bind('jqplotDataClick',
    function(ev, seriesIndex, pointIndex, data) {
        $('#info3').html('series: ' + seriesIndex + ', point: ' + pointIndex + ', data: ' + data);
    }
  );	
}
// hanshujiewei

$(document).ready(function() {
	try{
    $("#cid").hide();
    $("#chart").hide();
    var dataFrmTable = createDataFrmTable("chart");
    var data = DBFormat(dataFrmTable);   
        
        //var data = new stackData();
	drawStackChart(data);
}
catch(err){alert(err);}
            
});